<?
require_once '../../system/init.php';

$events = new Events();
$add = $events->addEventAction();

$errors = $events->getErrors();


$folders = array_keys($events->getEventsFoldersFromStorage());
$eventsFolders = $events->getEventsFoldersFromDb();

$folders = array_diff($folders, $eventsFolders);



require_once '../inc/header.php';
// Страница только для залогиненных пользователей
Common::checkLogin($user);
?>

<div class="main">
    <div class="center">
        <ul class="menu">
            <li><a href="/index.php" class="btn ">Альбомы</a></li>
            <li><a href="/tv/all_events.php" class="btn btn-active">События</a></li>
        </ul>

        <div id="user-info" class="h">
            <span id="user-name"></span>
            <span id="user-lastname"></span>
        </div>
    </div>
    <div class="clear"></div>
    <fieldset><legend><h1>Добавление события</h1></legend></fieldset>

    <div id="add-form"style="width: 500px; margin: 0 auto;">
        <form method="post" enctype="multipart/form-data" onsubmit="return checkForm ( );">
            <table class="form" cellpadding="0" cellspacing="0">
                <tr>
                    <td class="field-name"><label for="event_name">Название</label>
                    <td><input type="text" name="event_name" id="event_name" class="w-400" onFocus="hideError(this.id);" /></td>
                    <td class="errors" id="event_name-error"></td>
                </tr>
                <tr>
                    <td class="field-name"><label for="event_author">Автор</label></td>
                    <td><input type="text" name="event_author" id="event_author" class="w-400" value="" /></td>
                     <td class="errors"></td>
                </tr>
                <tr>
                    <td class="field-name"><label for="event-date">Дата</label></td>
                    <td><input type="text" name="event_date" id="event-date" class="w-400 datepicker" value="<?=date('Y-m-d')?>" /></td>
                     <td id="event-date-error" class="errors"></td>
                </tr>
                <tr>
                    <td class="field-name"><label for="event-desc">Описание</label></td>
                    <td><textarea id="event-desc" name="event_desc" class="w-400 h-100" onFocus="hideError(this.id)"></textarea></td>
                     <td class="errors" id="event-desc-error"></td>
                </tr>
                <tr>
                    <td class="field-name">Фотографии</td>
                    <td class="middle">
                        <input type="file" value="" name="photos[]" onClick="hideError(this.id)" id="files-upload" multiple>
                        
                        <? if (!empty($folders)): ?>
                            <span class="relative folders-events">или папка с ними&nbsp;&nbsp;</span>
                            <select name="event_folder" id="event_folder" onClick="hideError(this.id)">
                                <option value="" selected>--</option>
                                <? foreach ($folders as $folder): ?>
                                <option value="<?=$folder?>"><?=$folder?></option>
                                <? endforeach ?>
                            </select>
                        <? endif ?>
                    </td>
                    <td class="errors" id="files-upload-error"></td>
                </tr>
                <tr>
                    <td class="field-name"></td>
                    <td><label><input type="checkbox" id="event-rotate" name="event_rotate" checked /> Ротировать</label></td>
                </tr>
                <tr>
                    <td class="field-name"></td>
                    <td><input type="submit" name="submit" class="btn photo-info-save" /></td>
                </tr>
            </table>
        </form>
    </div>
</div>

<script>
function validate(){
        
        var form = $('#add-form').find('form').serialize();
        
        alert(params);
         
        $.post("/ajax_photoDelete.php", params , function (data) {
                    if (data.message) {
                        $('body').find('.photo-' + id).animate({ backgroundColor: "#fbc7c7" }, "fast")
        .animate({ opacity: "hide" }, "slow")
                    } else {
                        alert(data.message);
                    }
        });          
}

function checkForm() {
	var nameEvent    = $("#event_name"), 
		descEvent    = $("#event-desc"),
		//dateEvent    = $("#event-date"),
		photosEvents = $("input[type=file]"),
		folderEvents = $("#event_folder");
	if (nameEvent.val() && (photosEvents.val() || $("#event_folder").val()) /*&& dateEvent.val()*/ && descEvent.val().length <=100) {
		return true;
	} else {
		if (!nameEvent.val()) {
			nameEvent.css('border','1px #ff6600 solid');
			$("#event_name-error").text('введите название события');
		}
		
		//if (!dateEvent.val()) {
		//	dateEvent.css('border','1px #ff6600 solid');
		//	$("#event-date-error").text('введите дату события');
		//} 
		
		//if (!descEvent.val()) {
		//	descEvent.css('border','1px red solid');
		//	$("#event-desc-error").text('Введите описание события!');
		//}
        
        if( descEvent.val().length >100){
            descEvent.css('border','1px #ff6600 solid');
            $("#event-desc-error").text('Не более 100 символов');    
        }
        
		if (!photosEvents.val() && !$("#event_folder").val()) {
			$("#files-upload-error").text('нет выбранных файлов');
		}
		
		return false;
	} 
}
   
function hideError(inputFieldId) {
	if (inputFieldId == 'event_folder') {
		inputFieldId = 'files-upload';
	}
	
	$("#"+inputFieldId+"-error").text('');
	if (inputFieldId != 'files-upload') {
		$("#"+inputFieldId).css('border','1px #ccc solid');
	}
	
}
</script>


<script>
$(function(){
    $.datepicker.setDefaults(
        $.extend($.datepicker.regional["ru"])
    );
    $(".datepicker").datepicker({
        dateFormat:'yy-mm-dd'
    });
});

</script>


<? require_once '../inc/footer.php'; ?>